package user;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zbs
 * @date 2020/10/26
 */
@RestController
public class UserController {
    //http://localhost:9002/user?access_token=
    @RequestMapping("/user")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String user(Authentication authentication){
        //Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)authentication.getDetails();
        String token = details.getTokenValue();
        System.out.println("token:"+token);
        String username = authentication.getPrincipal().toString();
        return "这是client-user,用户："+username+",token:"+token;
    }
    @RequestMapping("/auth")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public Authentication auth(Authentication authentication){
        return authentication;
    }
}
